package com.yanqu.road.server.cmd.newmonopolymkt.office;

import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.enums.eMonopolyMarketResType;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.newmonopolymkt.NewMonopolyMarketProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.newmonopolymkt.NewMonopolyMarketModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.systemgroup.newmonopolymarket.NewMonopolyMarketMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;

@Cmd(code = GameProtocol.S_NEW_MONOPOLY_MARKET_OFFICE_GET_REWARD_REPORT, desc = "领取榷署奖励")
public class MonopolyMarketOfficeGetRewardReportCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if(!NewMonopolyMarketMgr.canEnterSystem(player)){
            ret = GameErrorCode.E_SYSTEM_NO_OPEN;
            return;
        }
        //周日22点直接限制不让查看
        if(!NewMonopolyMarketMgr.isSystemOpenTime(eMonopolyMarketResType.OFFICE_NEW.getValue())){
            ret = GameErrorCode.E_MONOPOLY_MARKET_SYSTEM_NO_OPEN;
            return;
        }
        // 领取奖励（周日22点后，不让手动領取？？）
        NewMonopolyMarketMgr.refreshUser(player,true,false);
        NewMonopolyMarketModule marketModule = player.getModule(NewMonopolyMarketModule.class);

        NewMonopolyMarketProto.NewMonopolyMarketGetQueShuRewardRespMsg.Builder builder = NewMonopolyMarketProto.NewMonopolyMarketGetQueShuRewardRespMsg.newBuilder();
        if(marketModule.userData.getTeaLastNum() > 0){
            Property reward = new Property(GameConfig.NEWMONOPOLYMARKET_GUEST_TEA_ID, BigInteger.valueOf(marketModule.userData.getTeaLastNum()));
            marketModule.userData.setTeaLastNum(0);
            player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.NewMonopolyMarket, eLogMoneyType.NewMonopolyMarketOfficeTeaReward);

            builder.setReward(PropertyHelper.parsePropertyToString(reward));
            marketModule.synRoleData();
        }
        builder.setRet(0);
        player.sendPacket(ClientProtocol.U_NEW_MONOPOLY_MARKET_OFFICE_GET_REWARD_REPORT, builder);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            NewMonopolyMarketProto.NewMonopolyMarketGetQueShuRewardRespMsg.Builder builder = NewMonopolyMarketProto.NewMonopolyMarketGetQueShuRewardRespMsg.newBuilder();
            builder.setRet(ret);
            player.sendPacket(ClientProtocol.U_NEW_MONOPOLY_MARKET_OFFICE_GET_REWARD_REPORT, builder);
        }
    }

}
